<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/5/29
 * Time: 19:54
 * 音视频合成
 */
require_once dirname(__DIR__)."/common.php";
use app\cls\tom\Lock;
use app\cls\tom\FFmpeg;
use app\lib\db\DbException;
use app\cls\tom\SysResource;
use app\cls\tom\Process;
use app\cls\tom\Tools;
use app\model\UserMaterial;

$process = new Process(__FILE__);
$process->isTryError =false;
//正式个数
$process->setProcessNum(5);
//测试个数
//$process->setProcessNum(3);
$process->initProcess();
$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);
$ffmpeg = new FFmpeg(true);
$lock = new Lock("wangchun_activity_");
/*===============
 *初始化模型
 ================*/
$userMaterialModel = new UserMaterial($db);
$cosUploadConfig = \app\cls\tom\Config::load("cos_upload");
while(true){
    try{
        $readyList = $userMaterialModel->getcronwangchun(10);
        $date1=date("Y",time());
        $date2=date("m",time());
        $date3=date("d",time());
        $time_dir=$date1."/".$date2."/".$date3;
        foreach($readyList as &$info){
            $lockStatus = $lock->tryLock($info["id"]."1");
//            $lock->unLock($info["id"]);exit;
            if($lockStatus){
                $videoUrl = $info["video_url"];
//                var_dump($videoUrl);exit;

                $outPutPath2 = CDN_PATH."/transCodeV1/".$time_dir."/".pathinfo($videoUrl,PATHINFO_BASENAME);
                $outPutPath1 = CDN_TMP1.$time_dir."/".pathinfo($videoUrl,PATHINFO_BASENAME);
                $oriVideoPath = str_replace(CDN_HOST,CDN_PATH,$videoUrl);
                $re = $ffmpeg->transCode($oriVideoPath,$outPutPath1,$outPutPath2,2);
//                var_dump($re);exit;
                if(!$re){
                    Tools::writeLog(["msg"=>"有音频视频转码失败","detail"=>["media_id"=>$info["id"],"msg"=>$ffmpeg->getLastError()],"ori_mix"]);
                    $db->update("wangchun_activity",["mix_status"=>1,"mix_msg"=>$ffmpeg->getLastError(),"mix_count"=>"!mix_count+1"],["id"=>$info["id"]]);
                    $lock->unLock($info["id"]);
                    continue;
                }
                $gaibian_video=str_replace(CDN_PATH,CDN_HOST,$outPutPath2);
                $db->reconnect();
                $db->update("wangchun_activity",["wangchun_video_url"=>$gaibian_video,"mix_status"=>2,"mix_msg"=>$ffmpeg->getLastError(),"mix_count"=>"!mix_count+1"],["id"=>$info["id"]]);

                $lock->unLock($info["id"]);
            }
        }
        sleep(2);
        $db->reconnect();
    }catch (DBException $e){
        $process->restartProcess();
    }catch (RedisException $e){

    }catch (Exception $e){
        if(!empty($info)){
            $db->update("wangchun_activity",["mix_status"=>2,"mix_msg"=>$e->getMessage(),"mix_count"=>"!mix_count+1"],["id"=>$info["id"]]);
            $lock->unLock($info["id"]);
        }
        Tools::writeLog(["msg"=>"音视频混音失败","detail"=>["msg"=>$e->getMessage(),"id"=>isset($info["id"])?$info["id"]:""],"mix"]);
    }
    $process->checkProcessTime(true);
}
